﻿using Exceptionless;
using Exceptionless.Dependency;
using Exceptionless.Logging;
using Exceptionless.Serializer;
using Exceptionless.Storage;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace WorkerServiceDemo
{
    public static class ExceptionlessExtensions
    {
        //
        // 摘要:
        //     Sets the configuration from .net configuration settings.
        //
        // 参数:
        //   config:
        //     The configuration object you want to apply the settings to.
        //
        //   settings:
        //     The configuration settings
        public static void ReadFromConfiguration(this ExceptionlessConfiguration config, IConfiguration settings)
        {
            ExceptionlessConfiguration config2 = config;
            if (config2 == null)
            {
                throw new ArgumentNullException("config");
            }

            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }

            IConfigurationSection section = settings.GetSection("Exceptionless");
            if (bool.TryParse(section["Enabled"], out bool result) && !result)
            {
                config2.Enabled = false;
            }

            string text = section["ApiKey"];
            if (!string.IsNullOrEmpty(text) && text != "API_KEY_HERE")
            {
                config2.ApiKey = text;
            }

            string text2 = section["ServerUrl"];
            if (!string.IsNullOrEmpty(text2))
            {
                config2.ServerUrl = text2;
            }

            if (TimeSpan.TryParse(section["QueueMaxAge"], out TimeSpan result2))
            {
                config2.QueueMaxAge = result2;
            }

            if (int.TryParse(section["QueueMaxAttempts"], out int result3))
            {
                config2.QueueMaxAttempts = result3;
            }

            string storagePath = section["StoragePath"];
            if (!string.IsNullOrEmpty(storagePath))
            {
                config2.Resolver.Register(typeof(IObjectStorage), () => new FolderObjectStorage(config2.Resolver, storagePath));
            }

            string text3 = section["StorageSerializer"];
            if (!string.IsNullOrEmpty(text3))
            {
                try
                {
                    Type type = Type.GetType(text3);
                    if (!typeof(IStorageSerializer).GetTypeInfo().IsAssignableFrom(type))
                    {
                        config2.Resolver.GetLog().Error(typeof(ExceptionlessConfigurationExtensions), $"The storage serializer {text3} does not implemented interface {typeof(IStorageSerializer)}.");
                    }
                    else
                    {
                        config2.Resolver.Register(typeof(IStorageSerializer), type);
                    }
                }
                catch (Exception ex)
                {
                    config2.Resolver.GetLog().Error(typeof(ExceptionlessConfigurationExtensions), ex, "The storage serializer " + text3 + " type could not be resolved: $" + ex.Message);
                }
            }

            if (bool.TryParse(section["EnableLogging"], out bool result4) && result4)
            {
                string text4 = section["LogPath"];
                if (!string.IsNullOrEmpty(text4))
                {
                    config2.UseFileLogger(text4);
                }
                else if (!string.IsNullOrEmpty(storagePath))
                {
                    config2.UseFileLogger(Path.Combine(storagePath, "exceptionless.log"));
                }
            }

            if (bool.TryParse(section["IncludePrivateInformation"], out bool result5) && !result5)
            {
                config2.IncludePrivateInformation = false;
            }

            if (bool.TryParse(section["ProcessQueueOnCompletedRequest"], out bool result6) && result6)
            {
                config2.ProcessQueueOnCompletedRequest = true;
            }

            foreach (IConfigurationSection child in section.GetSection("DefaultTags").GetChildren())
            {
                config2.DefaultTags.Add(child.Value);
            }

            foreach (IConfigurationSection child2 in section.GetSection("DefaultData").GetChildren())
            {
                if (child2.Value != null)
                {
                    config2.DefaultData[child2.Key] = child2.Value;
                }
            }

            foreach (IConfigurationSection child3 in section.GetSection("Settings").GetChildren())
            {
                if (child3.Value != null)
                {
                    config2.Settings[child3.Key] = child3.Value;
                }
            }
        }
    }
}
